#include "Player.h"

Player::Player(double x, double y, double z, int hitPoints, Projectile* projectile) : Camera(Camera::FIRST_PERSON)
{
    this->linearSpeed() = 50;
    this->position(x,y,z);
    this->hitPoints = hitPoints;
    this->activeBody();
    this->holdsJump = false;
    this->holdsSpeed = false;
    this->holdsGun = false;
    this->holdsAny = false;
    this->jumpActive = false;
    this->speedActive = false;
    this->canJump = false;
    body->friction() = 0.8;
    this->projectile = projectile;
    this->rotationSpeed() = 0;


}

Player::~Player()
{
    //dtor
}

void Player::act()
{
    if(Input::Keyboard::hit(KEYBOARD_SPACE) && this->canJump)
    {
        //If player holds the jump powerup to the mega jump
        if(this->jumpActive)
        {
            this->body->vel = (this->frustum->dist *500);
        }
        //do a normal jump otherwise
        else
        {
            this->body->vel = Math::Array(0,1,0) * 65;
        }

        this->canJump = false;
    }

    //Toggle jump On/Off
    if(this->holdsJump && Input::Keyboard::hit(KEYBOARD_J))
    {
        if(this->jumpActive)
        {
            this->jumpActive = false;
        }
        else
        {
            this->jumpActive = true;
        }
    }

    if(this->holdsGun && (Mouse::hit(MOUSE_LEFT) || Input::Keyboard::hit(KEYBOARD_L)))
    {
        printf("Pew!");
        this->projectile->visible() = true;
        this->projectile->position() = this->position();
        this->projectile->body->vel = (Mouse::ray() * 100);
    }

    //Toggle speed On/Off
    if(this->holdsSpeed && Input::Keyboard::hit(KEYBOARD_K))
    {
        if(this->speedActive)
        {
            this->speedActive = false;
            this->linearSpeed() = 50;
        }
        else
        {
            this->speedActive = true;
            this->linearSpeed() = 100;
        }
    }

    printGUI();
    checkGameOver();

}

bool Player::getHoldsGun()
{
return this->holdsGun;
}

bool Player::checkGameOver()
{
    if(this->hitPoints <= 0)
    {
        return true;
    }

    return false;
}

void Player::printGUI()
{

        //Text::write(50, 200, "Holds any? %d",this->holdsAny);
        //Text::write(50, 220, "Jump Active? %d",this->jumpActive);

        //Text::writeFPS(50,30);

        Text::write(50,50,"HP: %d", this->hitPoints);

        //Prints according to jump item ownership
        if(this->holdsAny)
        {
            if(this->holdsJump)
            {
                if(!this->jumpActive)
                {
                    Text::write(50,90, "j - Activate Jump Powerup");
                }
                else
                {
                    Text::write(50,90, "j - Deactivate Jump Powerup");
                }

            }

             if(this->holdsSpeed)
            {
                if(!this->speedActive)
                {
                    Text::write(50,110, "k - Activate Kinetic Powerup");
                }
                else
                {
                    Text::write(50,110, "k - Deactivate Kinetic Powerup");
                }

            }
            if(this->holdsGun)
            {
                Text::write(50,130, "LeftMouse - Shoot!");
            }
        }
        Text::write(170, 50,"Frags: %d", projectile->nFrags);
}

void Player::dropGun()
{
    this->holdsGun = false;
}

int Player::collide(Object& other)
{
    //if(other->)
    //Text::write(100,100, "Bateeeeeu!");

    // 0 - Terrain; 3 - Enemy
    if(other.type() != 0 && other.type()!= 4)
    {
        this->holdsAny = true;

        //2 - Speed Powerup
        if(other.type() == 2)
        {
            this->holdsSpeed = true;

        }//1 - Jump Powerup
        else if(other.type() == 1)
        {
            this->holdsJump = true;
        }//3 - Gun Powerup
        else if(other.type() == 3)
        {
            this->holdsGun = true;
        }

        other.visible() = 0;
        other.inactiveBody();
        //delete &other; <- ver qual eh o problema aqui...
    }

    //Collision with the enemy, uh oh...
    if(other.type() == 4)
    {
        printf("Fuuuuuck!\n");
        this->hitPoints -= 4;
    }

    if(other.type() == 0)
    {
        this->canJump = true;
    }

    return 1;
}

int Player::getHitPoints()
{
    return this->hitPoints;
}

